package fr.snac.bm;

import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ViewFlipper;
import fr.snac.utils.Constants;
import fr.snac.utils.DateUtil;

public class SwipeGestureDetector extends SimpleOnGestureListener{

	int minimumDistance;
	BonjourActivity activity;
	ViewFlipper flipper;


	public SwipeGestureDetector(BonjourActivity activity, ViewFlipper flipper, int minimumDistance) {
		this.minimumDistance = minimumDistance;
		this.activity = activity;
		this.flipper = flipper;
	}

	@Override
	public boolean onFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY){

		View v = activity.flipper.getCurrentView();

		if (me1.getX() - me2.getX() > minimumDistance){

			if (v.getTag() != null && v.getTag().equals(activity.today)) return true;
			
			activity.direction = Constants.DIRECTION_NEXT;
			activity.currentDate = DateUtil.dayAfter(DateUtil.getCalendarDate(activity.currentDate));
			
			if (activity.isInFlipper(activity.currentDate)){
				flipper.setInAnimation(activity, R.anim.in_left);
				flipper.setOutAnimation(activity, R.anim.out_left);
				flipper.showNext();
				activity.swipeEnabled = true;

				if (activity.isCurrentFavourite()){
					activity.menuFavourite.setIcon(android.R.drawable.star_on);
					activity.menuFavourite.setEnabled(false);
				}
				else{
					activity.menuFavourite.setIcon(android.R.drawable.star_off);
					activity.menuFavourite.setEnabled(true);
				}
			}else{
				if (!activity.swipeEnabled){
					if (!activity.isNetworkConnected()){
						activity.currentDate = DateUtil.dayBefore(DateUtil.getCalendarDate(activity.currentDate));
						return false;
					}
						
					activity.flipperFlush();
					activity.swipeEnabled = true;
				}
				activity.asyncTask = new AsyncSearch(activity);
				activity.asyncTask.execute(DateUtil.getCalendarDate(activity.currentDate));
			}


		}else if (me2.getX() - me1.getX() > minimumDistance){

			if (v.getTag() != null && v.getTag().equals(Constants.OLDEST_POST)) return true;

			activity.direction = Constants.DIRECTION_PREV;
			activity.currentDate = DateUtil.dayBefore(DateUtil.getCalendarDate(activity.currentDate));
						
			if (activity.isInFlipper(activity.currentDate)){
				flipper.setInAnimation(activity, R.anim.in_right);
				flipper.setOutAnimation(activity, R.anim.out_right);
				flipper.showPrevious();
				activity.swipeEnabled = true;
				
				if (activity.isCurrentFavourite()){
					activity.menuFavourite.setIcon(android.R.drawable.star_on);
					activity.menuFavourite.setEnabled(false);
				}
				else{
					activity.menuFavourite.setIcon(android.R.drawable.star_off);
					activity.menuFavourite.setEnabled(true);
				}
			}else{
				if (!activity.swipeEnabled){
					if (!activity.isNetworkConnected()){
						activity.currentDate = DateUtil.dayAfter(DateUtil.getCalendarDate(activity.currentDate));
						return false;
					}
					activity.flipperFlush();
					activity.swipeEnabled = true;
				}
				activity.asyncTask = new AsyncSearch(activity);
				activity.asyncTask.execute(DateUtil.getCalendarDate(activity.currentDate));
			}
		}

		return true;
	}

	

}
